package com.ozo.cloud.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ozo.cloud.common.base.model.BaseBatchDTO;
import com.ozo.cloud.common.base.model.BasePage;
import com.ozo.cloud.common.base.model.PageResult;
import com.ozo.cloud.common.core.exception.ServiceException;
import com.ozo.cloud.common.database.util.SortUtils;
import com.ozo.cloud.system.exception.SystemExceptionEnum;
import com.ozo.cloud.system.pojo.dto.ChangeStatusDTO;
import com.ozo.cloud.system.pojo.dto.SysDictDataQueryDTO;
import com.ozo.cloud.system.pojo.dto.SysDictDataSaveDTO;
import com.ozo.cloud.system.entity.SysDictData;
import com.ozo.cloud.system.mapper.SysDictDataMapper;
import com.ozo.cloud.system.service.ISysDictDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * <p>
 * 数据字典 服务实现类
 * </p>
 *
 * @author qiangesoft
 * @since 2024-07-11
 */
@Service
public class SysDictDataServiceImpl extends ServiceImpl<SysDictDataMapper, SysDictData> implements ISysDictDataService {

    @Override
    public void add(SysDictDataSaveDTO saveDTO) {
        // 参数校验
        this.checkParam(null, saveDTO.getDictTypeId(), saveDTO.getDictItem());

        SysDictData sysDictData = BeanUtil.copyProperties(saveDTO, SysDictData.class);
        baseMapper.insert(sysDictData);
    }

    /**
     * 合法性校验
     *
     * @param id
     * @param dictTypeId
     * @param dictItem
     */
    private void checkParam(Long id, Long dictTypeId, String dictItem) {
        LambdaQueryWrapper<SysDictData> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysDictData::getDictTypeId, dictTypeId)
                .eq(SysDictData::getDictItem, dictItem)
                .ne(Objects.nonNull(id), SysDictData::getId, id);
        Long count = baseMapper.selectCount(queryWrapper);
        if (count > 0) {
            throw new ServiceException(SystemExceptionEnum.DICT_ITEM_EXISTS);
        }
    }

    @Override
    public void remove(BaseBatchDTO<Long> batchDTO) {
        baseMapper.deleteBatchIds(batchDTO.getIdList());
    }

    @Override
    public void edit(SysDictDataSaveDTO saveDTO) {
        // 参数校验
        this.checkParam(saveDTO.getId(), saveDTO.getDictTypeId(), saveDTO.getDictItem());

        SysDictData sysDictData = BeanUtil.copyProperties(saveDTO, SysDictData.class);
        baseMapper.updateById(sysDictData);
    }

    @Override
    public PageResult<SysDictData> page(BasePage basePage, SysDictDataQueryDTO queryDTO) {
        Integer pageNum = basePage.getPageNum();
        Integer pageSize = basePage.getPageSize();

        QueryWrapper<SysDictData> queryWrapper = new QueryWrapper<>();
        // 排序
        String order = queryDTO.getOrder();
        if (StrUtil.isBlank(order)) {
            queryWrapper.lambda().orderByAsc(SysDictData::getOrderNum);
        } else {
            SortUtils.setSortCondition(queryWrapper, queryDTO.getProp(), order);
        }
        // 条件
        queryWrapper.lambda()
                .eq(Objects.nonNull(queryDTO.getDictTypeId()), SysDictData::getDictTypeId, queryDTO.getDictTypeId())
                .like(StrUtil.isNotBlank(queryDTO.getDictItem()), SysDictData::getDictItem, queryDTO.getDictItem())
                .eq(StrUtil.isNotBlank(queryDTO.getStatus()), SysDictData::getStatus, queryDTO.getStatus());
        Page<SysDictData> page = baseMapper.selectPage(new Page<>(pageNum, pageSize), queryWrapper);

        PageResult<SysDictData> pageResult = new PageResult<>();
        pageResult.setPageNum((int) page.getCurrent());
        pageResult.setPageSize((int) page.getSize());
        pageResult.setPage(page.getPages());
        pageResult.setTotal(page.getTotal());
        pageResult.setResults(page.getRecords());
        return pageResult;
    }

    @Override
    public void changeStatus(ChangeStatusDTO statusDTO) {
        LambdaUpdateWrapper<SysDictData> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SysDictData::getId, statusDTO.getId())
                .set(SysDictData::getStatus, statusDTO.getStatus());
        baseMapper.update(updateWrapper);
    }

}
